<?php

namespace Api\Cola;

use Api\Middleware\ColaAuthMiddleware;
use Api\Request\Cola\ColaAuthRequest;
use Api\Service\ColaAuthService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middlewares;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

#[AutoController]
class AuthController extends ColaBaseController
{

    /**
     * @var ColaAuthService
     */
    #[Inject]
    protected $authService;


    /**
     * 登录.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function login(): \Psr\Http\Message\ResponseInterface
    {
        $account = $this->request->input('account');
        $password = $this->request->input('password');
        $token = $this->authService->login($account, $password);

        return $this->response->success('success', ['token' => $token]);
    }

    /**
     * 注册.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function register(ColaAuthRequest $request): \Psr\Http\Message\ResponseInterface
    {
        $nickname = $this->request->input('nickname');
        $mobile = $this->request->input('mobile');
        $email = $this->request->input('email');
        $password = $this->request->input('password');
        $code = $this->request->input('code');
        $ip = $this->request->ip();
        $this->authService->register($mobile, $password,
            ['email' => $email, 'nickname' => $nickname, 'ip' => $ip]
        );
        return $this->response->success('注册成功');
    }

    /**
     * 退出登录.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[Middlewares([ColaAuthMiddleware::class])]
    public function logout(): \Psr\Http\Message\ResponseInterface
    {
        $this->authService->logout();
        return $this->response->success('退出登录');
    }

    /**
     * 用户信息.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[Middlewares([ColaAuthMiddleware::class])]
    public function userinfo(): \Psr\Http\Message\ResponseInterface
    {
        $userId = $this->request->input('user_id');
        return $this->response->success('success',);
    }

}